package com.agileboot.domain.performance.submission.model;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
 
import com.agileboot.domain.performance.submission.command.AddSubmissionCommand;
import com.agileboot.domain.performance.submission.command.UpdateSubmissionCommand;
import com.agileboot.domain.performance.submission.db.SubmissionEntity;
import com.agileboot.domain.performance.submission.db.SubmissionService;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import lombok.NoArgsConstructor;

/**
 * 绩效上报项模型
 */
@NoArgsConstructor
public class SubmissionModel extends SubmissionEntity {

    private SubmissionService submissionService;

    public SubmissionModel(SubmissionService submissionService) {
        this.submissionService = submissionService;
    }

    public SubmissionModel(SubmissionEntity entity, SubmissionService submissionService) {
        if (entity != null) {
            BeanUtil.copyProperties(entity, this);
        }
        this.submissionService = submissionService;
    }

    public void loadFromAddCommand(AddSubmissionCommand addCommand) {
        if (addCommand != null) {
            BeanUtil.copyProperties(addCommand, this, "id");
            if (StrUtil.isBlank(getAllocationDetails())) {
                setAllocationDetails("[]");
            }
            // 获取当前登录用户ID并设置creatorId和owner
            Long userId = AuthenticationUtils.getUserId();
            this.setCreatorId(userId);
            this.setOwner(userId.intValue());
        }
    }

    public void loadFromUpdateCommand(UpdateSubmissionCommand updateCommand) {
        if (updateCommand != null) {
            loadFromAddCommand(updateCommand);
        }
    }

    public void checkCanBeDeleted() {
        // 这里可以根据实际需求添加删除前的验证逻辑
    }

    public void checkTitleUnique() {
        // 这里可以加入判断标题唯一性的逻辑
    }

}
